package com.hzjh.base.style.util

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.util.Log
import android.view.View
import android.view.Window
import android.view.WindowManager
import java.lang.Exception
import kotlin.math.abs

/**
 * 状态栏的工具
 */
class StatusBarUtils {
    companion object{
        fun setWindowStatusBarColor(activity: Activity, colorResId: Int) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    val window = activity.window
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                    window.statusBarColor = activity.resources.getColor(colorResId)

                    //底部导航栏
                    //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        /**
         * 设置状态栏字体颜色
         * @param activity    activity
         * @param isDark      isdark
         */
        fun setTextColorStatusBar(activity: Activity, isDark: Boolean) {
            FlymeSetStatusBarLightMode(activity.window, isDark)
            MIUISetStatusBarLightMode(activity.window, isDark)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isDark) { //6.0
                activity.window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

        /**
         * 设置状态栏图标为深色和魅族特定的文字风格
         * 可以用来判断是否为Flyme用户
         *
         * @param window 需要设置的窗口
         * @param dark   是否把状态栏字体及图标颜色设置为深色
         * @return boolean 成功执行返回true
         */
        private fun FlymeSetStatusBarLightMode(window: Window?, dark: Boolean): Boolean {
            var result = false
            if (window != null) {
                try {
                    val lp = window.attributes
                    val darkFlag = WindowManager.LayoutParams::class.java
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
                    val meizuFlags = WindowManager.LayoutParams::class.java
                        .getDeclaredField("meizuFlags")
                    darkFlag.isAccessible = true
                    meizuFlags.isAccessible = true
                    val bit = darkFlag.getInt(null)
                    var value = meizuFlags.getInt(lp)
                    value = if (dark) {
                        value or bit
                    } else {
                        value and bit.inv()
                    }
                    meizuFlags.setInt(lp, value)
                    window.attributes = lp
                    result = true
                } catch (e: Exception) {
                }
            }
            return result
        }

        /**
         * 设置状态栏字体图标为深色，需要MIUIV6以上
         *
         * @param window 需要设置的窗口
         * @param dark   是否把状态栏字体及图标颜色设置为深色
         * @return boolean 成功执行返回true
         */
        private fun MIUISetStatusBarLightMode(window: Window?, dark: Boolean): Boolean {
            var result = false
            if (window != null) {
                val clazz: Class<*> = window.javaClass
                try {
                    var darkModeFlag = 0
                    val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
                    val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
                    darkModeFlag = field.getInt(layoutParams)
                    val extraFlagField = clazz.getMethod(
                        "setExtraFlags",
                        Int::class.javaPrimitiveType,
                        Int::class.javaPrimitiveType
                    )
                    if (dark) {
                        extraFlagField.invoke(window, darkModeFlag, darkModeFlag) //状态栏透明且黑色字体
                    } else {
                        extraFlagField.invoke(window, 0, darkModeFlag) //清除黑色字体
                    }
                    result = true
                } catch (e: Exception) {
                }
            }
            return result
        }

        /**
         * 获取状态栏高度   默认4.4以下返回状态栏高度为0
         * @param context con
         * @return  状态栏高度
         */
        fun getStatusHeight(context: Context): Int {
            var statusBarHeight = 0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                // 获取status_bar_height资源的ID
                val resourceId =
                    context.resources.getIdentifier("status_bar_height", "dimen", "android")
                if (resourceId > 0) {
                    // 根据资源ID获取响应的尺寸值
                    statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
                }
            }
            return statusBarHeight
        }

        /**
         * 状态栏透明
         * 未实现虚拟按钮的隐藏，界面覆盖虚拟按钮，
         * 虚拟按钮可响应事件暂未解决
         * @param activity activity
         */
        fun translateStatusBar(activity: Activity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                val window = activity.window
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window.statusBarColor = Color.TRANSPARENT
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                val localLayoutParams = activity.window.attributes
                localLayoutParams.flags =
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or localLayoutParams.flags
            }
        }

        /**
         * 判断颜色是深色还是浅色
         * @param color true=light false=dark
         * @return
         */
        fun isLightColor(color: Int): Boolean {
            var color = color
            color = abs(color)
            val darkness =
                1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(
                    color
                )) / 255
            return darkness < 0.5
        }

        fun getViewBackground(view: View): Int {
            return if (view.background is ColorDrawable) {
                (view.background as ColorDrawable).color
            } else {
                -1 //代表白色
            }
        }
    }
}